package unit9_9.pojo;

import java.sql.*;

public class MySql1 {
    public static void main(String[] args) {
//        String address="jdbc:mysql://连接地址:3306/数据库名称?useSSL=false&serverTimezone=UTC";
        String address="jdbc:mysql://localhost:3306/school?useSSL=false&serverTimezone=UTC";
        String userName="root";
        String pwd="123456";
        Connection connection=null;
//        Connection connection= DriverManager.getConnection(address,userName,pwd);


        try {
//            加载JDBC驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("驱动加载成功");
//            连接数据库
            connection= DriverManager.getConnection(address,userName,pwd);
            System.out.println("连接成功");
//            处理数据
            String sql="select * from stu";

            Statement statement=connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);

            resultSet.next();
            System.out.println(resultSet.getString("sname"));

        } catch (Exception e) {
            System.out.println("连接失败");
            e.printStackTrace();
        }finally {
            try {
//                关闭连接资源
                System.out.println("关闭连接");
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
    }
}
